package com.jetinno.utils

import android.annotation.TargetApi
import android.content.Context
import android.content.res.Configuration
import android.content.res.Resources
import android.os.Build
import timber.log.Timber
import java.util.*

/**
 * Created by zhangyuncai on 2020/11/13.OK
 */
object LocalManageUtil {

    @JvmStatic
    var useAppLangType = false

    /*--------------------------------↓↓↓↓↓↓↓↓↓↓APP语言环境↓↓↓↓↓↓↓↓↓↓--------------------------------*/

    //当前APP语言 "zh_CN"表示简体中文 "en_"表示英文
    private var appLanguage by Preference("language", LangType.chinese.code)

    /**
     * 获取当前语言环境的Locale
     */
    @JvmStatic
    fun getAppLangType(): LangType {
        return LangType.findTypeByCode(appLanguage)
    }

    @JvmStatic
    fun setAppLanguage2(langType: LangType) {
        appLanguage = langType.code
    }

    @JvmStatic
    fun getAppLangCode(): String = getAppLangType().code

    /*--------------------------------↑↑↑↑↑↑↑↑↑↑APP语言环境↑↑↑↑↑↑↑↑↑↑--------------------------------*/
    /*--------------------------------↓↓↓↓↓↓↓↓↓↓售卖语言环境↓↓↓↓↓↓↓↓↓↓--------------------------------*/

    //当前APP售卖语言 "CN"表示中文 "EN"表示中文
    private var frontLanguage by Preference("front_language", LangType.chinese.code)

    /**
     * 设置售卖语言环境的Locale
     */
    @JvmStatic
    fun setFrontLanguage2(type: LangType) {
        frontLanguage = type.code;
    }

    /**
     * 获取售卖语言环境的Locale
     */
    @JvmStatic
    fun getFrontLangType(): LangType {
        return LangType.findTypeByCode(frontLanguage)
    }

    /*--------------------------------↑↑↑↑↑↑↑↑↑↑售卖语言环境↑↑↑↑↑↑↑↑↑↑--------------------------------*/
    /*--------------------------------↓↓↓↓↓↓↓↓↓↓以下是修改APP语言环境的代码↓↓↓↓↓↓↓↓↓↓--------------------------------*/

    /**
     * 修改当前语言
     * @param applicationContext 必须用当前context中获取applicationCotnext对象,禁止传application
     * @param newLocaleLang CN:简体中文 EN:英文
     */
    @JvmStatic
    fun changeAppLanguage(applicationContext: Context) {
        val targetLocale: Locale
        if (useAppLangType) {
            targetLocale = getAppLangType().locale
        } else {
            targetLocale = getFrontLangType().locale
        }
        val resources = applicationContext.resources
        val configuration = resources.configuration
        configuration.setLocale(targetLocale)
        //为什么不用createConfigurationContext,因为无效 https://www.jianshu.com/p/e6542e33a126
        resources.updateConfiguration(configuration, resources.displayMetrics)
    }

    fun attachBaseContextLang(newBase: Context): Context {
        /* 设置默认语言或者之前已经选择的语言 */
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return createConfigurationContext(newBase)
        }
        setConfiguration(newBase);
        return newBase
    }

    /**
     * 设置语言
     */
    fun setConfiguration(newBase: Context) {
        val targetLocale: Locale
        if (useAppLangType) {
            targetLocale = getAppLangType().locale
        } else {
            targetLocale = getFrontLangType().locale
        }
        val configuration: Configuration = newBase.getResources().getConfiguration()
        configuration.setLocale(targetLocale)
        val resources: Resources = newBase.getResources()
        val dm = resources.displayMetrics
        resources.updateConfiguration(configuration, dm) //语言更换生效的代码!
    }

    /**
     * 更新语言
     * 适配7.0以上
     */
    @TargetApi(Build.VERSION_CODES.N)
    private fun createConfigurationContext(context: Context): Context {
        val targetLocale: Locale
        if (useAppLangType) {
            targetLocale = getAppLangType().locale
        } else {
            targetLocale = getFrontLangType().locale
        }
        Timber.e("targetLocale:" + targetLocale.language)
        val config = context.resources.configuration
        config.setLocale(targetLocale)
        return context.createConfigurationContext(config)
    }

}